package fudan.se.lab2.service;

import fudan.se.lab2.domain.Contribution;
import fudan.se.lab2.domain.Invitation;
import fudan.se.lab2.domain.Mark;
import fudan.se.lab2.domain.Meeting;
import fudan.se.lab2.repository.ContributionRepository;
import fudan.se.lab2.repository.InvitationRepository;
import fudan.se.lab2.repository.MarkRepository;
import fudan.se.lab2.repository.MeetingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserUtilService {
    private final MeetingRepository meetingRepository;
    private final MarkRepository markRepository;
    private final InvitationRepository invitationRepository;
    private final ContributionRepository contributionRepository;

    @Autowired
    public UserUtilService(MeetingRepository meetingRepository, MarkRepository markRepository, InvitationRepository invitationRepository, ContributionRepository contributionRepository) {
        this.meetingRepository = meetingRepository;
        this.markRepository = markRepository;
        this.invitationRepository = invitationRepository;
        this.contributionRepository = contributionRepository;
    }

    public List<Meeting> meetingIApplied(String userName) {
        Iterable<Meeting> it = meetingRepository.findMeetingByChair(userName);
        List<Meeting> meetingIApplied = new ArrayList<>();
        int meetingStatus;
        for (Meeting meeting : it) {
            meetingStatus = meeting.getStatus();
            if (meetingStatus == 0 || meetingStatus == 1 || meetingStatus == 2 || meetingStatus == 3 || meetingStatus == 4) {
                meetingIApplied.add(meeting);
            }
        }
        return meetingIApplied;
    }

    public List<Meeting> meetingIEnded(String userName) {
        Iterable<Meeting> it = meetingRepository.findMeetingByChair(userName);
        List<Meeting> meetingIEnded = new ArrayList<>();
        for (Meeting meeting : it) {
            if (meeting.getStatus() == 5) {
                meetingIEnded.add(meeting);
            }
        }
        return meetingIEnded;
    }

    public List<Meeting> meetingIJoined(String userName) {
        Iterable<Invitation> it = invitationRepository.findByToUsername(userName);
        List<Meeting> meetingIJoined = new ArrayList<>();
        for (Invitation invitation : it) {
            if (invitation.getStatus() == 1) {
                meetingIJoined.add(meetingRepository.findMeetingByFullName(invitation.getMeetingFullName()));
            }
        }
        return meetingIJoined;
    }

    public List<Contribution> meetingIContributed(String userName) {
        Iterable<Contribution> it = contributionRepository.findByContributor(userName);
        List<Contribution> meetingIContributed = new ArrayList<>();
        for (Contribution contribution : it) {
            int meetingStatus = meetingRepository.findMeetingByFullName(contribution.getMeetingFullName()).getStatus();
            contribution.setMeetingStatus(meetingStatus);
            contributionRepository.save(contribution);
            meetingIContributed.add(contribution);
        }
        return meetingIContributed;
    }

    public List<Meeting> meetingInvitedMe(String userName) {
        Iterable<Invitation> it = invitationRepository.findByToUsername(userName);
        List<Meeting> meetingInvitedMe = new ArrayList<>();
        for (Invitation invitation : it) {
            if (invitation.getStatus() == 0) {
                meetingInvitedMe.add(meetingRepository.findMeetingByFullName(invitation.getMeetingFullName()));
            }
        }
        return meetingInvitedMe;
    }

    public List<Mark> marksIGotInThisMeeting(String userName) {
        List<Mark> marks = new ArrayList<>();
        Iterable<Contribution> contributions = contributionRepository.findByContributor(userName);
        for (Contribution contribution : contributions) {
            Meeting meeting = meetingRepository.findMeetingByFullName(contribution.getMeetingFullName());
            if (meeting.getStatus() == 4) {
                Mark mark = markRepository.findByContributorAndTitle(userName, contribution.getFileTitle());
                mark.setMeetingFullName(meeting.getFullName());
                markRepository.save(mark);
                marks.add(mark);
            }
        }
        return marks;
    }
}
